package com.yuju.controller.login;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.yuju.common.PageModel;
import com.yuju.common.Result;
import com.yuju.common.fileld.validator.FieldValidator;
import com.yuju.controller.base.BaseController;
import com.yuju.enums.ReturnCodeEnum;
import com.yuju.enums.ReturnStatusEnum;
import com.yuju.group.enums.HotelReturnCodeEnum;
import com.yuju.login.enums.OpHotelReturnCodeEnum;
import com.yuju.login.model.OpHotelModel;
import com.yuju.login.service.IOpHotelService;
import com.yuju.util.Utils;
import com.yuju.vo.login.OpHotelVO;

/**
 * 
 * 类名：OpHotelController  <br />
 *
 * 功能：
 *
 * @author : zhousc <br />
 * @Date : 2017年12月4日 上午11:00:11  <br />
 * @version : 1.1 <br />
 */
@Controller
@RequestMapping("opHotel")
public class OpHotelController extends BaseController<OpHotelVO, OpHotelModel> {

	@Autowired
	private IOpHotelService opHotelService;
	
	/**
	 * 
	 * 功能：<br/>
	 *
	 * @author zhousc
	 * @Date 2017年12月4日 上午11:00:15 <br/>
	 */
	@RequestMapping("list")
	@ResponseBody
	public Result list(@RequestBody JSONObject obj) {
		OpHotelVO  opHotelVO = obj.toJavaObject(OpHotelVO.class);
		// 参数校验
		Map<String, String> fail = FieldValidator.validateAll(opHotelVO, "pager", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		PageModel pageModel = opHotelService.pageList(convert(opHotelVO), opHotelVO.getPage(), opHotelVO.getPageSize());
		if (Utils.isNotNull(pageModel) && Utils.isNotNull(pageModel.getRows())) {
			result.setPageModel(pageModel);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(OpHotelReturnCodeEnum.OPHOTEL_QUERY_SUCCESS.getCode());
			result.setReturnMsg(OpHotelReturnCodeEnum.OPHOTEL_QUERY_SUCCESS.getDesc());
		} else {
			result = new Result();
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		}
		return result;
	}
	
	/**
	 * 
	 * 功能：<br/>
	 *
	 * @author zhousc
	 * @Date 2017年12月4日 上午11:00:19 <br/>
	 */
	@RequestMapping("save")
	@ResponseBody
	public Result save(@RequestBody JSONObject obj) {
		OpHotelVO  opHotelVO = obj.toJavaObject(OpHotelVO.class);
		Result result = new Result();
		int rows = opHotelService.save(convert(opHotelVO));
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(OpHotelReturnCodeEnum.OPHOTEL_SAVE_SUCCESS.getCode());
			result.setReturnMsg(OpHotelReturnCodeEnum.OPHOTEL_SAVE_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(OpHotelReturnCodeEnum.OPHOTEL_SAVE_FAILURE.getCode());
			result.setReturnMsg(OpHotelReturnCodeEnum.OPHOTEL_SAVE_FAILURE.getDesc());
		}
		return result;
	}
	
	/**
	 * 
	 * 功能：<br/>
	 *
	 * @author zhousc
	 * @Date 2017年12月4日 上午11:00:22 <br/>
	 */
	@RequestMapping("update")
	@ResponseBody
	public Result update(@RequestBody JSONObject obj) {
		OpHotelVO  opHotelVO = obj.toJavaObject(OpHotelVO.class);
		Result result = new Result();
		int rows = opHotelService.updateSelective(convert(opHotelVO));
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(OpHotelReturnCodeEnum.OPHOTEL_UPDATE_SUCCESS.getCode());
			result.setReturnMsg(OpHotelReturnCodeEnum.OPHOTEL_UPDATE_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(OpHotelReturnCodeEnum.OPHOTEL_UPDATE_FAILURE.getCode());
			result.setReturnMsg(OpHotelReturnCodeEnum.OPHOTEL_UPDATE_FAILURE.getDesc());
		}
		return result;
	}

	/**
	 * 
	 * 功能：<br/>
	 *
	 * @author zhousc
	 * @Date 2017年12月4日 上午11:00:26 <br/>
	 */
	@RequestMapping("delete")
	@ResponseBody
	public Result delete(@RequestBody JSONObject obj) {
		OpHotelVO  opHotelVO = obj.toJavaObject(OpHotelVO.class);
		Result result = new Result();
		int rows = opHotelService.del(opHotelVO.getId());
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(OpHotelReturnCodeEnum.OPHOTEL_DEL_SUCCESS.getCode());
			result.setReturnMsg(OpHotelReturnCodeEnum.OPHOTEL_DEL_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(OpHotelReturnCodeEnum.OPHOTEL_DEL_FAILURE.getCode());
			result.setReturnMsg(OpHotelReturnCodeEnum.OPHOTEL_DEL_FAILURE.getDesc());
		}
		return result;
	}
	
	/**
	 * 
	 * 功能：<br/>
	 *
	 * @author zhousc
	 * @Date 2017年12月4日 上午11:00:31 <br/>
	 */
	@RequestMapping("get")
	@ResponseBody
	public Result get(@RequestBody JSONObject obj) {
		OpHotelVO  opHotelVO = obj.toJavaObject(OpHotelVO.class);
		Result result = new Result();
		OpHotelModel OpHotelModel = opHotelService.findById(opHotelVO.getId());
		if (Utils.isNotNull(OpHotelModel)) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setData(OpHotelModel);
			result.setReturnCode(OpHotelReturnCodeEnum.OPHOTEL_QUERY_SUCCESS.getCode());
			result.setReturnMsg(OpHotelReturnCodeEnum.OPHOTEL_QUERY_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		}
		return result;
	}
	
	/**
	 * 
	 * 功能：获取账户所属酒店<br/>
	 *
	 * @author xuehj
	 * @param obj 参数集
	 * @Date 2018年1月17日 上午11:22:06 <br/>
	 */
	@RequestMapping("getHotelsByOp")
	@ResponseBody
	public Result getHotelsByOp(@RequestBody JSONObject obj) {
		//根据账号ID查询分店权限范围
		List<OpHotelModel> opHotellist = opHotelService.hotelListByOpId(obj.getString("opId"));
		
		Result result = new Result();
		if (Utils.isNull(opHotellist) || opHotellist.isEmpty()) {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
			return result;
		}
		
		result.setRows(opHotellist);
		
		result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
		result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
		result.setReturnCode(HotelReturnCodeEnum.Hotel_QUERY_SUCCESS.getCode());
		result.setReturnMsg(HotelReturnCodeEnum.Hotel_QUERY_SUCCESS.getDesc());
		return result;
	}
}
